package jsyf.oa.service.device;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import jsyf.com.util.FastJsonConvert;
import jsyf.oa.dao.device.DeviceModelDao;
import jsyf.oa.entity.device.DeviceModelEntity;
import jsyf.oa.facade.device.DeviceModelFacade;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.List;

/**
 * Created by jjx on 2018/3/8.
 */
@Service
@Transactional(rollbackFor = Exception.class)
@com.alibaba.dubbo.config.annotation.Service(protocol = {"dubbo"})
public class DeviceModelService implements DeviceModelFacade {


    @Autowired
    private DeviceModelDao deviceModelDao;

    @Override
    public JSONObject getModelList(String jsonData) {

        JSONObject jsonParams = FastJsonConvert.convertJSONToObject(jsonData, JSONObject.class);
        //品牌名称
        String brandName = jsonParams.getString("brandName");
        //品牌名称id
        String brandId = jsonParams.getString("brandId");
        //模型
        String modelName = jsonParams.getString("modelName");
        //模型id
        String modelId = jsonParams.getString("modelId");
        //操作时间
        String createTime = jsonParams.getString("createTime");
        //操作人员
        String createUser = jsonParams.getString("createUser");

        //分页信息
        int requestPage = 1;
        int pageSize = Integer.MAX_VALUE;

        if (jsonParams.containsKey("pageSize")) {
            pageSize = jsonParams.getInteger("pageSize");
        }
        if (jsonParams.containsKey("requestPage")) {
            requestPage = jsonParams.getInteger("requestPage");
        }
        //传参
        StringBuffer condition = new StringBuffer();
        List<Object> sqlArgs = new ArrayList<Object>();
        if (!com.alibaba.dubbo.common.utils.StringUtils.isBlank(brandName)) {
            condition.append(" AND brandName=?");
            sqlArgs.add(brandName);
        }
        if (!com.alibaba.dubbo.common.utils.StringUtils.isBlank(brandId)) {
            condition.append(" AND brandId=?");
            sqlArgs.add(brandId);
        }
        if (!com.alibaba.dubbo.common.utils.StringUtils.isBlank(modelName)) {
            condition.append(" AND modelName=?");
            sqlArgs.add(modelName);
        }
        if (!com.alibaba.dubbo.common.utils.StringUtils.isBlank(modelId)) {
            condition.append(" AND modelId=?");
            sqlArgs.add(modelId);
        }
        if (!com.alibaba.dubbo.common.utils.StringUtils.isBlank(createTime)) {
            condition.append(" AND createTime=?");
            sqlArgs.add(createTime);
        }
        if (!com.alibaba.dubbo.common.utils.StringUtils.isBlank(createUser)) {
            condition.append(" AND createUser like ?");
            sqlArgs.add("%" + createUser + "%");
        }
        condition.append("AND delFlag=0");
        condition.append(" ORDER BY createTime DESC");
        //查询list
        List<JSONObject> records = deviceModelDao.getViewJSONListByPage(condition.toString(), sqlArgs.toArray(), requestPage, pageSize);
        //查询总数
        Integer recordCount = deviceModelDao.getViewPageCount(condition.toString(), sqlArgs.toArray());

        //分装前端需要的格式
        int pageCount = 0;
        if (recordCount < pageSize) {
            pageCount = 1;
        } else {
            double flag = Math.ceil(recordCount / pageSize + 1);
            pageCount = ((int) flag);
        }
        if (recordCount == 0) {
            pageCount = 0;
        }
        //存放分页信息
        JSONObject sendData = new JSONObject();
        //存放list
        JSONObject result = new JSONObject();
        sendData.put("recordCount", recordCount);
        sendData.put("pageCount", pageCount);
        sendData.put("requestPage", 1);
        result.put("pager", sendData);
        // 子数组对象
        result.put("records", records);
        return result;

    }

    @Override
    public List<JSONObject> getModelByModelName(String jsonData) {
        JSONObject jsonParams = FastJsonConvert.convertJSONToObject(jsonData, JSONObject.class);
        String modelName = jsonParams.getString("modelName");
        String modelNameUpper = modelName.toUpperCase();

        StringBuffer sbCondition = new StringBuffer();
        String modelLowPrice = "";
        BigDecimal bgPrice;
        sbCondition.append(" and MODEL_NAME='" + modelNameUpper + "' ");
        sbCondition.append(" and DEL_FLAG = 0");
        List<JSONObject> modelList = deviceModelDao.getJSONListByCondition(sbCondition.toString(), new Object[]{});
        if (modelList.size() > 0) {
            for (int i = 0; i < modelList.size(); i++) {
                modelLowPrice = modelList.get(i).getString("lowestPrice");
                bgPrice = (new BigDecimal(modelLowPrice)).divide(new BigDecimal(10000), 2, BigDecimal.ROUND_HALF_UP);
                modelList.get(i).put("lowestPrice", bgPrice);
            }
        }

        return modelList;

    }


    @Override
    public List<JSONObject> getBucketCapacityList(String jsonData) {
        String brandId = new String(JSON.parseObject(jsonData).getString("brandId"));
        String modelId = new String(JSON.parseObject(jsonData).getString("modelId"));
        return deviceModelDao.getBucketCapacityList(brandId, modelId);
    }

    @Override
    public JSONObject getById(String jsonData) {
        return deviceModelDao.getJSONById(JSON.parseObject(jsonData).getString("id"));
    }

    @Override
    public Boolean update(String jsonData) {
        DeviceModelEntity deviceModelEntity = JSON.toJavaObject(JSON.parseObject(jsonData), DeviceModelEntity.class);
        return deviceModelDao.update(deviceModelEntity) > 0;
    }

    @Override
    public Boolean delById(String jsonData) {
        return deviceModelDao.delById(JSON.parseObject(jsonData).getString("id")) > 0;
    }

    @Override
    public Boolean removeById(String jsonData) {
        return deviceModelDao.removeById(JSON.parseObject(jsonData).getString("id")) > 0;
    }

    @Override
    public JSONObject getAllModel(String jsonData) {
        StringBuffer sql = new StringBuffer();
        JSONObject jsonParams = FastJsonConvert.convertJSONToObject(jsonData, JSONObject.class);
        String brandName = jsonParams.getString("brandName");
        String modelName = jsonParams.getString("modelName");
        String brandId = jsonParams.getString("brandId");
        sql.append(" LEFT JOIN T_DVC_BRAND ON T_DVC_MODEL.BRAND_ID=T_DVC_BRAND.ID WHERE T_DVC_MODEL.DEL_FLAG=0");
        if (!StringUtils.isBlank(brandName)) {
            brandName = brandName.trim();
            if (!"请选择".equals(brandName)) {
                sql.append(" AND T_DVC_BRAND.BRAND_NAME = " + "'" + brandName + "'");
            }

        }
        if (!StringUtils.isBlank(modelName)) {
            modelName = modelName.trim();
            if (!"请选择".equals(modelName)) {
                sql.append(" AND T_DVC_MODEL.MODEL_NAME = " + "'" + modelName + "'");
            }
        }
        if (!StringUtils.isBlank(brandId)) {
            brandId = brandId.trim();
            if (!"请选择".equals(brandId)) {
                sql.append(" AND T_DVC_MODEL.BRAND_ID = " + "'" + brandId + "'");
            }

        }
        String requestPage;
        String pageSize;
        if (jsonParams.containsKey("requestPage") && jsonParams.containsKey("pageSize")) {
            requestPage = jsonParams.getString("requestPage");
            pageSize = jsonParams.getString("pageSize");
        } else {
            requestPage = "1";
            pageSize = Integer.MAX_VALUE + "";
        }
        if (StringUtils.isBlank(requestPage) && StringUtils.isBlank(pageSize)) {
            requestPage = "1";
            pageSize = Integer.MAX_VALUE + "";
        }
        List<JSONObject> records = deviceModelDao.getJSONListByPage1(sql.toString(), new Object[]{}, Integer.valueOf(requestPage), Integer.valueOf(pageSize));
        int recordCount = deviceModelDao.getPageCount(sql.toString(), new Object[]{});
        int recordSize = Integer.valueOf(pageSize);
        //如果整除表示正好分N页，如果不能整除在N页的基础上+1页
        int pageCount = recordCount % recordSize == 0 ? recordCount / recordSize : (recordCount / recordSize) + 1;
        JSONObject result = new JSONObject();
        JSONObject objSendData = new JSONObject();
        //子对象
        objSendData.put("recordCount", recordCount);
        objSendData.put("pageCount", pageCount);
        objSendData.put("requestPage", 1);
        result.put("pager", objSendData);
        // 子数组对象
        result.put("records", records);
        return result;
    }

    /**
     * 自己修改吧！！！！
     **/
   /* @Override
    public JSONObject getLow(String jsonData) {
        String bucketCapacity = new String(JSON.parseObject(jsonData).getString("bucketCapacity"));
        String modelId = new String(JSON.parseObject(jsonData).getString("modelId"));
        return deviceModelDao.getLow(bucketCapacity,modelId);
    }*/
    @Override
    public String add(String jsonData) {
        JSONObject JSONObject = JSON.parseObject(jsonData);
        DeviceModelEntity deviceModelEntity = JSON.toJavaObject(JSONObject, DeviceModelEntity.class);
        deviceModelEntity.setDelFlag(0);
        deviceModelEntity.setSortNo(0);
        deviceModelDao.add(deviceModelEntity);
        return deviceModelEntity.getId();

    }

    @Override
    public JSONObject getBrandListByTypeId(String jsonData) {
        JSONObject JSONObject = JSON.parseObject(jsonData);
        JSONObject object = new JSONObject();
        String deviceTypeId = JSONObject.getString("deviceTypeId");
        String companyId = JSONObject.getString("companyId");
        List<JSONObject> brands = deviceModelDao.getBrandListBytypeId(companyId);
        object.put("brands", brands);
        return object;
    }

    @Override
    public JSONObject getBrand(String jsonData) {
        JSONObject JSONObject = JSON.parseObject(jsonData);
        JSONObject object = new JSONObject();
        List<JSONObject> brands = deviceModelDao.getBrand();
        object.put("brands", brands);
        return object;
    }

    @Override
    public JSONObject getModelListByBrandId(String jsonData) {
        JSONObject JSONObject = JSON.parseObject(jsonData);
        String brandId = JSONObject.getString("brandId");
        String deviceTypeId = JSONObject.getString("deviceTypeId");
        JSONObject object = new JSONObject();
        List<JSONObject> models = deviceModelDao.getModelListBybrandId(brandId, deviceTypeId);
        object.put("models", models);
        return object;
    }

    //只通过BrandId获取对应的品牌
    @Override
    public JSONObject getModelListByBId(String jsonData) {
        JSONObject JSONObject = JSON.parseObject(jsonData);
        String brandId = JSONObject.getString("brandId");
        JSONObject object = new JSONObject();
        List<JSONObject> models = deviceModelDao.getModelListByBId(brandId);
        object.put("models", models);
        return object;
    }

    @Override
    public JSONObject getBrandByCompanyId(String jsonData) {
        JSONObject JSONObject = JSON.parseObject(jsonData);
        String companyId = JSONObject.getString("companyId");
        JSONObject object = new JSONObject();
        List<JSONObject> brands = deviceModelDao.getBrandByCompanyId(companyId);
        object.put("brands", brands);
        return object;
    }
}
